package ExamSystemClient.client;

import ExamSystemClient.model.ChoiceQuestion;
import ExamSystemClient.model.Student;
import ExamSystemClient.model.User;
import ExamSystemClient.model.UserMessage;

import java.io.IOException;
import java.util.List;

/**
 * 实现客户端的主界面绘制和相应的功能实现
 */
public class ClientView {
    /**
     * 为了可以使用输入输出流，采用合成复合原则
     */
    private ClientInitClose cic;

    /**
     * 实现成员变量的初始化
     * @param cic
     */
    public ClientView(ClientInitClose cic){
        this.cic = cic;
    }

    /**
     * 实现客户端主界面的绘制
     */
    public void clientMainPage() throws IOException, ClassNotFoundException {
        while (true) {
            System.out.println("  \n\n\t\t    在线考试系统");
            System.out.println("---------------------------------");
            System.out.print("    [1]学员系统");
            System.out.println("    [2]管理员系统");
            System.out.println("    [0]退出系统");
            System.out.println("---------------------------------");
            System.out.println("请选择要进行的业务编号：");

            int choose = ClientScanner.getScanner().nextInt();
            switch (choose) {
                case 1:
                    System.out.println("正在进入学员系统");
                    cic.getOos().writeObject("student");
                    if (! clientLogin(false)){
                        break;
                    }
                    Student student= (Student) cic.getOis().readObject();
                    List<ChoiceQuestion> questions= (List<ChoiceQuestion>) cic.getOis().readObject();
                    StudentSystem ss = new StudentSystem(student, questions);
                    ss.studentOperate();
                    cic.getOos().writeObject(ss.getStudent());
                    break;
                case 2:
                    System.out.println("正在进入管理员系统");
                    cic.getOos().writeObject("manager");
                    if (! clientLogin(true)){
                        break;
                    }
                    //执行管理员操作
                    List<Student> students= (List<Student>) cic.getOis().readObject();
                    questions= (List<ChoiceQuestion>) cic.getOis().readObject();
                    ManagerSystem ms = new ManagerSystem(questions, students);
                    ms.managerOperate();
                    //执行完操作将新的students和questions返给服务器
                    cic.getOos().writeObject(ms.getStudents());
                    cic.getOos().writeObject(ms.getQuestions());
                    break;
                case 0:
                    System.out.println("正在退出系统。。。");
                    cic.getOos().writeObject("exit");
                    return;
                default:
                    System.out.println("输入错误，请重新选择！");
            }
        }
    }

    /**
     * 用户（管理员、学员）登录
     * @param manager
     * @return
     * @throws IOException
     * @throws ClassNotFoundException
     */
    private boolean clientLogin(boolean manager) throws IOException, ClassNotFoundException {
        //1、准备管理员登录的相关数据
        System.out.println("请输入" + (manager ? "管理员" : "学员") + "的账户信息：");
        String userName = ClientScanner.getScanner().next();
        System.out.println("请输入"+ (manager ? "管理员" : "学员") +"的密码信息：");
        String password = ClientScanner.getScanner().next();
        UserMessage tum = new UserMessage((manager ?"managerCheck" : "studentCheck"), new User(userName, password));
        //2、将UserMaessage类别的对象通过对象输出流发送给服务器
        cic.getOos().writeObject(tum);
        System.out.println("客户端发送" +  (manager ? "管理员" : "学员") +"账户信息成功！");
        //3、接受服务器的处理结果并给出提示
        tum = (UserMessage) cic.getOis().readObject();
        if ("success".equals(tum.getType())){
            System.out.println("登录成功，欢迎使用！");
            return true;
        }else{
            System.out.println("用户名或密码错误");
            return false;
        }
    }


}
